package com.my.algorithmlearning.leetcode;

import org.springframework.util.Assert;

/**
 * @ClassName HammingDistance
 * @Description HammingDistance
 * @Author wanghaiwei
 * @Date 2023/1/17 17:19
 */
public class HammingDistance {
    public int hammingDistance(int x, int y) {
        int keepDiffOne = x ^ y;
        int count = 0;
        while (keepDiffOne != 0) {
            keepDiffOne = keepDiffOne & (keepDiffOne - 1);
            count++;
        }
        return count;
    }
    public static void main(String[] args) {
        HammingDistance solution = new HammingDistance();
        int result = solution.hammingDistance(1, 4);
        Assert.isTrue(2 == result, "x = 1, y = 4计算错误,预期:2,实际" + result);
        result = solution.hammingDistance(3, 1);
        Assert.isTrue(1 == result, "x = 3, y = 1计算错误,预期:1,实际" + result);
    }
}
